import 'dart:io';

import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/manager/xindong_beauty_singleton.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/beauty/xindong_beauty_utils_view.dart';
import 'package:xindong/routes/call/controller/xindong_call_rtc_controller.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_app_bar_icon_button.dart';
import 'package:tencent_trtc_cloud/trtc_cloud_def.dart';
import 'package:tencent_trtc_cloud/trtc_cloud_video_view.dart';

class XinDongBeautyYeMian extends StatefulWidget {
  const XinDongBeautyYeMian({super.key});

  @override
  State<XinDongBeautyYeMian> createState() => _XinDongBeautyYeMianState();
}

class _XinDongBeautyYeMianState extends State<XinDongBeautyYeMian> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      XinDongBeautyManager.instance.init();
      XinDongCallRTCController.to.init();
    });
  }

  @override
  void dispose() {
    XinDongCallRTCController.to.rtcCloud.stopLocalPreview();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.resumed:
        if (Platform.isAndroid) {
          // XinDongBeautyMethodChannel.setAppLifeState(1);
        }

        break;
      case AppLifecycleState.paused:
        if (Platform.isAndroid) {
          // XinDongBeautyMethodChannel.setAppLifeState(2);
        }
        break;
      case AppLifecycleState.detached:
        break;
      case AppLifecycleState.hidden:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        children: [
          // 本地预览
          TRTCCloudVideoView(
            key: const ValueKey("cameraVideoViewId"),
            viewType: TRTCCloudDef.TRTC_VideoView_TextureView,
            onViewCreated: (viewId) async {
              final controller = XinDongCallRTCController.to;
              await controller.rtcCloud.startLocalPreview(controller.isFrontCamera, viewId);
            },
          ),
          XinDongAppBar(
            "",
            leading: const XinDongAppBarIconButton(isWhite: true),
            backgroundColor: Colors.transparent,
            brightness: Brightness.dark,
            actions: [
              GestureDetector(
                onTap: () => XinDongBeautyManager.instance.save(),
                behavior: HitTestBehavior.opaque,
                child: SizedBox(
                  width: 32,
                  height: 32,
                  child: ImageUtil.asset(AssetsRes.CALL_BEAUTY_SAVE),
                ),
              ),
              // GestureDetector(
              //   onTapUp: (a) {
              //     // XinDongBeautyMethodChannel.setComparison(true);
              //   },
              //   onTapDown: (a) {
              //     // XinDongBeautyMethodChannel.setComparison(false);
              //   },
              //   behavior: HitTestBehavior.opaque,
              //   child: Container(
              //     width: 32,
              //     height: 32,
              //     // child: Image.asset("assets/beauty/icon_frame.png"),
              //   ),
              // ),
              const SizedBox(width: 10),
              GestureDetector(
                onTap: () => XinDongCallRTCController.to.switchCamera(),
                behavior: HitTestBehavior.opaque,
                child: SizedBox(
                  width: 32,
                  height: 32,
                  child: ImageUtil.asset(AssetsRes.CALL_CAMERA_REVERSE),
                ),
              ),
              const SizedBox(width: kPaddingValue),
            ],
          ),
          const Positioned(bottom: 0, left: 0, right: 0, child: XinDongBeautyToolsView(isEditing: true)),
          // Positioned(
          //   left: 0,
          //   right: 0,
          //   bottom: 0,
          //   child: Selector<XinDongSendFileProvider, BeautyState>(
          //       selector: (_, provider) => provider.beautyState,
          //       builder: (context, providerData, child) {
          //         return providerData==BeautyState.succeed
          //             ? Container(
          //           child: Column(
          //             children: [
          //               XinDongSliderContainer(),
          //               XinDongEffectTab(),
          //             ],
          //           ),
          //         )
          //             : Container(
          //           height: beautyViewHeight,
          //           decoration: const BoxDecoration(color: Color.fromRGBO(0, 0, 0, 0.7)),
          //           child: Material(
          //             color: Colors.transparent,
          //             child: XinDongBeautyLoadingWidget(beautyState: providerData,),
          //           ),
          //         );
          //       }),
          // ),
        ],
      ),
    );
  }
}
